#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Osh.Dvb
{
	public class TransportSubstream : PacketStream, IDvbPacketStreamFilter
	{
		private TransportStreamDemultiplexer _demultiplexor;
		private int _pid;
		private PacketStreamType _streamType;

		public TransportSubstream(TransportStreamDemultiplexer demultiplexor, int pid, PacketStreamType streamType)
		{
			_demultiplexor = demultiplexor;
			_pid = pid;
			_streamType = streamType;
		}

		public int Pid
		{
			get { return _pid; }
		}

		public PacketStreamType StreamType
		{
			get { return _streamType; }
		}

		#region IDvbPacketStreamFilter
		public void NextPacket(int pid, byte[] packet, int offset, int length)
		{
			base.FillPacketBuffer(packet, offset, length);
		}
		#endregion IDvbPacketStreamFilter

		#region PacketStreamBase Overrides
		protected override bool MoveToNextPacketInternal()
		{
			this.PacketSize = 0;
			while(true)
			{
				if(!_demultiplexor.Read())
					return false;

				if(this.PacketSize > 0)
					break;
			}
			return true;
		}

		public override void Close()
		{
			_demultiplexor.RemoveFilter(_pid, this);
			base.Close();
		}
		#endregion PacketStreamBase Overrides
	}

}
